﻿using ClientAppraise.Controls;
using ShareCode.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Wpf.Ui.Controls;

namespace ClientAppraise.Services.Impl
{
    public class ZxDrawerService : IZxDrawerService
    {
        private readonly List<KeyValuePair<int, ContentPresenter>> contents = [];

        public ZxDrawer? Drawer
        {
            get; private set;
        }
        public ContentPresenter? EjectContentPresenter(int HashCode)
        {
            var value = contents.FirstOrDefault(t => t.Key == HashCode);

            if (!value.Equals(default(KeyValuePair<int, ContentPresenter>)))
            {
                contents.Remove(value);
            }

            var myReturn = value.Equals(default(KeyValuePair<int, ContentPresenter>)) ? value.Value : null;

            return myReturn;
        }
        public ContentPresenter? GetContentPresenter(int HashCode)
        {
            var value = contents.FirstOrDefault(t => t.Key == HashCode);

            if (value.Equals(default(KeyValuePair<int, ContentPresenter>)))
            {
                if (contents.Count > 0)
                {
                    return contents[^1].Value;
                }
            }

            var myReturn = value.Equals(default(KeyValuePair<int, ContentPresenter>)) ? null : value.Value;

            return myReturn;
        }
        public ContentPresenter? GetContentPresenter()
        {
            var code = 0;
            foreach (Window window in Application.Current.Windows)
            {
                if (window.IsActive)//使用窗体类进行匹配查找
                {
                    code = window.GetHashCode();
                    break;
                }
            }

            return GetContentPresenter(code);
        }
        public void PushContentPresenter(int HashCode, ContentPresenter? contentPresenter)
        {
            if (contentPresenter == null) return;

            //contents.TryAdd(HashCode, contentPresenter);
            var value = contents.FirstOrDefault(t => t.Key == HashCode);

            if (value.Equals(default(KeyValuePair<int, ContentPresenter>)))
            {
                contents.Add(new KeyValuePair<int, ContentPresenter>(HashCode, contentPresenter));
            }
        }

        public void CloseDrawer(bool result)
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                System.Windows.Application.Current.Dispatcher.Invoke(() => CloseDrawer(result));
                return;
            }

            Drawer?.Hide(result);
        }
        public void CloseDrawer(ResultDto<object> result)
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                System.Windows.Application.Current.Dispatcher.Invoke(() => CloseDrawer(result));
                return;
            }

            Drawer?.Hide(result);
        }

        public async Task<ResultDto<object>> ShowDrawer(UserControl control)
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                return await Application.Current.Dispatcher.Invoke(() => ShowDrawer(control));
            }

            var Content = GetContentPresenter();
            if (Content == default)
            {
                throw new ArgumentNullException($"The ContentPresenter didn't set previously.");
            }

            try
            {
                Drawer = new ZxDrawer(Content);
                return await Drawer.ShowAsync(control);
            }
            finally
            {

            }

        }
        public Task<ResultDto<object>> ShowDrawer<T>() where T : UserControl
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                return Application.Current.Dispatcher.Invoke(() => ShowDrawer<T>());
            }
            return ShowDrawer<T>(out _);
        }
        public Task<ResultDto<object>> ShowDrawer<T>(out T? control) where T : UserControl
        {
            control = null;

            if (!App.Current.Dispatcher.CheckAccess())
            {
                return Application.Current.Dispatcher.Invoke(() => ShowDrawer<T>(out T? control));
            }

            control = App.GetRequiredService<T>();

            if (control == default)
            {
                throw new ArgumentNullException(nameof(control));
            }

            return ShowDrawer(control);
        }

        public Task<ResultDto<object>> ShowDrawer<T>(Action<T> action) where T : UserControl
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                return Application.Current.Dispatcher.Invoke(() => ShowDrawer<T>(action));
            }

            var control = App.GetRequiredService<T>();

            if (control == default)
            {
                throw new NullReferenceException(nameof(control));
            }

            if (action != default)
            {
                action.Invoke(control);
            }

            return ShowDrawer(control);
        }


    }
}
